import { defineStore } from 'pinia'
import * as userApi from '../../../api/system/user'
import type { UserInfo } from '../../../types/app.d'

// 用户管理 Store 接口
interface UserManagementState {
  userList: UserInfo[]
  total: number
  loading: boolean
  currentUser: UserInfo | null
}

export const useUserStore = defineStore('userManagement', {
  state: (): UserManagementState => ({
    userList: [],
    total: 0,
    loading: false,
    currentUser: null
  }),

  getters: {
    // 获取用户列表
    getUserList(): UserInfo[] {
      return this.userList
    },

    // 获取用户总数
    getUserTotal(): number {
      return this.total
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前用户
    getCurrentUser(): UserInfo | null {
      return this.currentUser
    }
  },

  actions: {
    // 获取用户列表
    async getUserListAction(params: {
      page: number
      pageSize: number
      username?: string
      nickname?: string
      status?: number
      deptId?: string
      beginTime?: string
      endTime?: string
    }): Promise<void> {
      this.loading = true
      try {
        const response = await userApi.getUserList(params)
        if (response.code === 200) {
          this.userList = response.data.list || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        console.error('获取用户列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取用户详情
    async getUserInfoAction(id: string): Promise<void> {
      try {
        const response = await userApi.getUserInfo(id)
        if (response.code === 200) {
          this.currentUser = response.data
        }
      } catch (error) {
        console.error('获取用户详情失败:', error)
      }
    },

    // 创建用户
    async createUserAction(data: Omit<UserInfo, 'id' | 'createTime' | 'updateTime'>): Promise<boolean> {
      try {
        const response = await userApi.createUser(data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('创建用户失败:', error)
        return false
      }
    },

    // 更新用户
    async updateUserAction(id: string, data: Partial<Omit<UserInfo, 'id' | 'createTime'>>): Promise<boolean> {
      try {
        const response = await userApi.updateUser(id, data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新用户失败:', error)
        return false
      }
    },

    // 删除用户
    async deleteUserAction(id: string): Promise<boolean> {
      try {
        const response = await userApi.deleteUser(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除用户失败:', error)
        return false
      }
    },

    // 批量删除用户
    async batchDeleteUserAction(ids: string[]): Promise<boolean> {
      try {
        const response = await userApi.batchDeleteUser(ids)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('批量删除用户失败:', error)
        return false
      }
    },

    // 修改用户状态
    async changeUserStatusAction(id: string, status: number): Promise<boolean> {
      try {
        const response = await userApi.changeUserStatus(id, status)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('修改用户状态失败:', error)
        return false
      }
    },

    // 重置用户密码
    async resetUserPasswordAction(id: string, password: string): Promise<boolean> {
      try {
        const response = await userApi.resetUserPassword(id, password)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('重置用户密码失败:', error)
        return false
      }
    },

    // 更新用户角色
    async updateUserRolesAction(id: string, roleIds: string[]): Promise<boolean> {
      try {
        const response = await userApi.updateUserRoles(id, roleIds)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新用户角色失败:', error)
        return false
      }
    },

    // 清空当前用户
    clearCurrentUser(): void {
      this.currentUser = null
    }
  }
})